package com;

import java.util.ArrayList;

import com.Exception.NotExistingException;

/**
 * Constante d'un atome.
 * Associé à un type
 * @author florent
 */
public class Constante{

	String name;        // nom / valeur de la constante
	Type type = null;   // type de la variable
	
	private static ArrayList<Constante> EXISTING_CONSTANTES = new ArrayList<Constante>();
	
	private Constante(String name,Type type){
		this.name=name;
		this.type=type;
	}
	
	public String getName(){
		return name;
	}
	
	public Type getType(){
		return type;
	}
	
	public String toString(){
		return name;
	}
	
	public static Constante newConstante(String name,Type type){
		boolean exists = false;
		
		name = name.replace(" ", "");
		
		Constante constante = null;
		
		for(Constante c : EXISTING_CONSTANTES){
			if(c.name.equals(name)){
				exists = true;
				constante = c;
				break;
			}
		}
		if(exists == false){
			constante = new Constante(name,type);
			EXISTING_CONSTANTES.add(constante);
		}
		
		return constante;
	}
	
	public static ArrayList<Constante> getConstantes(){
		return EXISTING_CONSTANTES;
	}
	
	public static Constante getConstante(String name) throws NotExistingException{
		for(Constante c : EXISTING_CONSTANTES){
			if(c.name.equals(name))
				return c;
		}
		throw new NotExistingException("Constante innexistante");
	}
}
